package com.gxw.www.myapp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.gxw.www.myapp.fragment.ContainerFragment;
import com.gxw.www.myapp.fragment.NavigationDrawerLeft;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

@SuppressLint("NewApi")
@ContentView(R.layout.activity_frag)
public class FragActivity extends SherlockFragmentActivity {

	/**
	 * 控件
	 */
	@ViewInject(R.id.drawer_layout)
	private DrawerLayout mDrawerLayout;// 画布

	@ViewInject(R.id.navigation_drawer_left)
	private FrameLayout mNavigationDrawerLeft;// 左滑菜单

	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 无窗口显示
		// requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 声明上下文
		context = this;

		// 初始化Xutils
		ViewUtils.inject(this);

		// 主Fragment
		ContainerFragment bodyFragment = new ContainerFragment();

		// 侧滑菜单
		NavigationDrawerLeft navigationDrawerLeftFragment = new NavigationDrawerLeft();

		// Fragment操作类
		FragmentManager fragmentManager = getSupportFragmentManager();

		// 替换fragment
		fragmentManager.beginTransaction()
				.replace(R.id.content_frame, bodyFragment).commit();

		// 添加bundle
		// navigationDrawerLeftFragment.setArguments(bundle);
		// 替换fragment
		fragmentManager
				.beginTransaction()
				.replace(R.id.navigation_drawer_left,
						navigationDrawerLeftFragment).commit();

		// 初始化actionbar
		initActionBar();

	}

	@OnClick({ R.id.btn_left })
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.btn_left:
			if (mDrawerLayout.isDrawerOpen(mNavigationDrawerLeft)) {
				mDrawerLayout.closeDrawer(Gravity.START);
			} else {
				mDrawerLayout.openDrawer(Gravity.START);
			}
			break;

		default:
			break;
		}

	}

	/**
	 * 初始化actionbar
	 */
	private void initActionBar() {
		// TODO Auto-generated method stub
		/**
		 * actionBar.setDisplayHomeAsUpEnabled(true)
		 * 给左上角图标的左边加上一个返回的图标。对应ActionBar.DISPLAY_HOME_AS_UP
		 */
		getSupportActionBar().setDisplayShowHomeEnabled(false);// 返回键不可见

		getSupportActionBar().setDisplayShowTitleEnabled(false);// 标题不可见

		/**
		 * getDrawable(int) 过时 Use {@link #getDrawable(int, Theme)} instead.
		 */
		getSupportActionBar()
				.setBackgroundDrawable(
						getResources().getDrawable(
								R.drawable.map_page_tittle_bg, null));

		/**
		 * 使自定义的普通View能在title栏显示，即actionBar.setCustomView能起作用。
		 * 对应ActionBar.DISPLAY_SHOW_CUSTOM
		 */
		getSupportActionBar().setDisplayShowCustomEnabled(true);

		// 获得actionbar的View
		View actionBarLayout = LayoutInflater.from(context).inflate(
				R.layout.view_actionbar, null);
		// 自定义actionbar样式
		getSupportActionBar().setCustomView(actionBarLayout);

		// // 获得点击按钮View
		// RelativeLayout btnLeft = (RelativeLayout) actionBarLayout
		// .findViewById(R.id.btn_left);
		//
		// // 设置按钮点击事件
		// btnLeft.setOnClickListener(this);
	}

}
